//提示信息
function prompt(msg){
	var prompt_box = document.getElementsByClassName("prompt_box")[0];
	if(!prompt_box){
		var html = "<div class='prompt_box'><span>"+msg+"</span></div>";
		$("body").append(html);
	}else{
		prompt_box.innerHTML = "<span>"+msg+"</span>";
	}
	$(".prompt_box").fadeIn(1500);
	$(".prompt_box").fadeOut(1500);
}

// 身份证校验
function identityCheck(id, value) {
	var reg = /^[0-9A-Za-z]*$/;
	var regC = /^[\u4e00-\u9fa5]+$/;
	var identity = /[0-9]|[xX]/;
	var identityReg = /^[1-9]{1}[0-9]{14}$|^[1-9]{1}[0-9]{16}([0-9]|[xX])$/;

	if (identity.test(value)) {
		if (value.length > 18) {
			value = value.slice(0, 18);
			$("#" + id).val(value);
		} else if (value.length < 18) {
			for (i = 0; i < value.length; i++) {
				var char = value.charAt(i);
				if (/[0-9]/.test(char)) {

				} else {
					value = value.slice(0, value.length - 1);
					$("#" + id).val(value);
				}
			}
		} else if (value.length == 18) {
			for (i = 0; i < value.length; i++) {
				var char = value.charAt(i);
				if (identity.test(char)) {

				} else {
					value = value.slice(0, value.length - 1);
					$("#" + id).val(value);
				}
			}
		}
	} else {
		value = value.slice(0, value.length - 1);
		value = value.split(value.match(/[\u4E00-\u9FA5]/i))[0];
		$("#" + id).val(value);
	}

}

//倒计时，手机验证码倒计时
var countdown=60;
var timer;
function settime(val) { 
	if (countdown <= 0) { 
		val.removeAttribute("disabled"); 
		val.value="获取验证码"; 
		countdown = 60; 
		return ;
	} else { 
		val.setAttribute("disabled", true); 
		val.value = "重新发送(" + countdown + ")"; 
		countdown--; 
	} 
	timer = setTimeout(function() { 
		settime(val) 
	},1000) 
} 

//delete,清空当前input值
function deletefunc(id){
	$("#"+id).val("")
}

var i = 0;

//获取图片验证码
// function imgVerificationCode(){
// 	 $.ajax({
// 	     type : 'POST',
// 	     contentType : 'application/json',
// 	     url:baseurl+"/v1/verify/img",
// 	     data : {},
// 	     dataType : 'json',
// 	     success : function(data) {
// 	     	 console.log(data.data);
// 	         imgKey = data.data.data.imgKey;  // 获取图片验证时返回的唯一标志
// 	         $(".yzm_img").attr("src","data:image/png;base64,"+data.data.data.imgBase64);
// 	     },
// 	     error : function(data) {
// 	         //alert("error");
// 	     }
// 	 });
// }


//刷新图片
$(".refreshImg").on("touchend",function(){
	$(".refreshImg").css({"transform":"rotateZ("+i*360+"deg)","transition":"all 1s"});
	i++;
	imgVerificationCode();
});


//时间戳转换
function timestampToTime(timestamp) {
    var date = new Date(timestamp);
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate())+ ' ';
    h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
    m = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes();
    s = date.getSeconds();
    
    return Y+M+D+h+m;
}

//时间戳转换
function timestampToTimeToDay(timestamp) {
    var date = new Date(timestamp);
    Y = date.getFullYear() + '-';
    M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    D = (date.getDate() < 10 ? '0'+date.getDate() : date.getDate())+ ' ';
    h = (date.getHours() < 10 ? '0'+date.getHours() : date.getHours()) + ':';
    m = date.getMinutes() < 10 ? '0'+date.getMinutes() : date.getMinutes();
    s = date.getSeconds();
    
    return Y+M+D;
}

//发短信验证码
$("#hqyzmBtni").click(function(){
	obj=$(this);
	// getCode(obj[0]);
    sendSms(obj[0]);
});
//发送短信验证码
function sendSms(obj){
    var that =  obj;
    var tel=$("#tel").val();
    if (tel==""||tel == null) {
        return prompt("手机号码不能为空");
    }
    if (!/^1[3|4|5|6|7|8|9]\d{9}$/.test(tel)) {
        return prompt("请输入11位正确的手机号码");
    }
    $.ajax({
        headers: {
            'Accept': "application/json; charset=utf-8",
            'Api-Version-Number': "v1.6.6"
        },
        type : "POST",
        url : baseUrl + '/api/user/verification_codes',
        data : {
            mobile:tel,
            type:'credit_card_handle'
        },
        success : function(result) {
            if (0 !== result.code) {
                return prompt(result.message);
            } else {
                settime(that);
            }
        }
    });
}
